package com.zenob.secalendar;

import com.zenob.secalendar.util.DateUtil;


/**
 * @author Thomas Oldervoll, thomas@zenior.no
 * @author $Author: batcage $
 * @version $Rev: 38 $
 * @date $Date: 2006-12-30 09:22:30 +0100 (So, 30 gru 2006) $
 */
public class GCalEvent {


    public String id;
    public String summary;
    public String note;
    public String location;
	public String origEventId; /* original recurrence event ID */
    public long updated;
    public long startTime;
    public long endTime;
    public boolean cancelled;
    public int reminder;
	public Recurrence recur;

	public GCalEvent()
	{
		this.id = "";
		this.summary = "";
		this.note = "";
		this.location = "";
		this.origEventId = "";
		this.recur = null;
	}

    public String asXML() {
        // TODO: note
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("<?xml version=\"1.0\"?>\n");
        stringBuffer.append("<entry xmlns=\"http://www.w3.org/2005/Atom\"\n" +
							"xmlns:gd=\"http://schemas.google.com/g/2005\">\n" +
							"<category scheme=\"http://schemas.google.com/g/2005#kind\"\n" +
							"term=\"http://schemas.google.com/g/2005#event\"></category>\n");

		//insert event ID
		if (this.id != null && this.id.equals("") == false)
			stringBuffer.append("<id>" + this.id + "</id>\n");

		//insert timestamp of update
		if (this.updated > 0) 
			stringBuffer.append("<updated>" + DateUtil.longToIsoDateTime(this.updated) + "</updated>\n");

		//insert event title
		stringBuffer.append("<title type=\"text\">" + this.summary + "</title>\n");

		//insert event status
		if (this.cancelled)
			stringBuffer.append("<gd:eventStatus value=\"http://schemas.google.com/g/2005#event.cancelled\" />" );

		//insert start and end times of event
		stringBuffer.append("<gd:when startTime=\"" + DateUtil.longToIsoDateTime(this.startTime) +
							"\" endTime=\"" + DateUtil.longToIsoDateTime(this.endTime) + "\">");

		//insert reminder for non-recurring event
		if (this.recur == null)
		{
			//reminder must be inside of <gd:when> for non-recurring events
			if (this.reminder > 0)
				stringBuffer.append("<gd:reminder minutes=\"" + this.reminder + "\"/>");
		}
		//close <gd:when>
		stringBuffer.append("</gd:when>\n");

		//insert repeat rule
		if (this.recur != null)
		{
			String recurStr = this.recur.toString();
			if (recurStr.equals("") == false)
			{
				stringBuffer.append("<gd:recurrence>" + recurStr + "</gd:recurrence>\n");

				//insert reminder for recurring event
				if (this.reminder > 0)
					stringBuffer.append("<gd:reminder minutes=\"" + this.reminder + "\" />\n");
			}
		}

		//insert event location
		if (this.location != null && this.location.equals("") == false)
			stringBuffer.append("<gd:where valueString=\"" + this.location + "\"/>");

		//close <entry>
		stringBuffer.append("</entry>");

		return stringBuffer.toString();
    }


    public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("What: \"" + this.summary + "\", ");
		sb.append("When: " + DateUtil.longToIsoDateTime(this.startTime) + " .. " + DateUtil.longToIsoDateTime(this.endTime) + ", ");
		if (this.location != null && !this.location.equals("")) sb.append("Where: " + this.location + ", ");
        if (this.note != null && !this.note.equals("")) sb.append("Note: \"" + this.note + "\", ");
		if (this.reminder > 0) sb.append("Reminder: " + this.reminder + " minutes, ");
		if (this.recur != null) sb.append("Repeats: " + this.recur.toReadableString());

        return sb.toString();
    }
}
